﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Model.ModelsExt;
using AntUnion.FrameWork.Model;
using AntUnion.FrameWork.Util;
using AntUnion.FrameWork.Web.Common;
using AntUnion.FrameWork.Web.Service;
using Microsoft.AspNetCore.Mvc;
using static AntUnion.FrameWork.Web.Service.AssetsServer;
using System.Collections.Generic;
using System;
using System.Linq;

namespace AntUnion.FrameWork.Web.Controllers
{
    public class DepositController : BaseController
    {
        #region 入金

        /// <summary>
        /// 入金
        /// </summary>
        /// <returns></returns>
        public IActionResult Deposit(string language = "user-zh-CN")
        {
            string defaultLanguage = "zh-CN";
            if (language == "user-en-US")
            {
                defaultLanguage = "en-US";
            }
            CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
            CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
            Mt5User mt5User = mt5Service.GetMt5UserById(crmUserModel.Id);
            ViewBag.hasMt5User = true;
            if (mt5User != null)
            {
                ViewBag.hasMt5User = false;
                ViewBag.login = mt5User.Login;
            }
            List<PublicService.SystemConfigModel> systemConfigModels = publicService.GetSystemConfig();
            ViewBag.rechargeWarmPrompt = systemConfigModels.FirstOrDefault(k => k.name == "rechargeWarmPrompt" && k.language == defaultLanguage).value;
            return View();
        }

        /// <summary>
        /// 获取电汇账户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult WireTransfer()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<WireTransfer> wireTransferList = assetsServer.GetWireTransfer(crmUserModel.RoleId);
                res.Code = 1;
                res.Data = wireTransferList;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取汇款账户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult RemittanceAccount()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<RemittanceAccount> remittanceAccountList = assetsServer.GetRemittanceAccount(crmUserModel.RoleId);
                res.Code = 1;
                res.Data = remittanceAccountList;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取加密货币
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult Cryptocurrency()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<Cryptocurrency> cryptoCurrencyList = assetsServer.GetCryptoCurrency(crmUserModel.RoleId);
                res.Code = 1;
                res.Data = cryptoCurrencyList;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 获取资金账户 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetMt5User()
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                List<Mt5UserModel> mt5UserModels = mt5Service.GetMt5User(crmUserModel.Id, (int)Mt5UserType.Real);
                res.Code = 1;
                res.Data = mt5UserModels;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        ///// <summary>
        ///// 获取资金账户 
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public JsonResult GetMt5User()
        //{
        //    Result res = new Result();
        //    try
        //    {
        //        CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
        //        Mt5UserModel mt5UserModel = mt5Service.GetMt5User(crmUserModel.Id, (int)Mt5UserType.Real).FirstOrDefault();
        //        res.Code = 1;
        //        res.Data = mt5UserModel;
        //        return new JsonResult(res);
        //    }
        //    catch (Exception ex)
        //    {
        //        res.Code = 1;
        //        res.Data = ex.Message;
        //        return new JsonResult(res);
        //    }
        //}

        /// <summary>
        /// 提交入金申请
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        //public JsonResult Deposit(int channelId, int mothod, int mt5Id, string depositcurrency, string fromcurrency, decimal fees, decimal depositamount, decimal fromamount, string certificate, string password, string remarks)
        //{
        //    Result res = new Result();
        //    try
        //    {
        //        CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
        //        DepositCollection obj = assetsServer.GetCollection(channelId, mothod);
        //        if (obj == null)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.collectionAccontNull;
        //            return new JsonResult(res);
        //        }
        //        CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
        //        //确认用户是否存在
        //        if (crmUser == null)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.crmUserNull;
        //            return new JsonResult(res);
        //        }
        //        if (string.IsNullOrEmpty(certificate))
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.certificateNull;
        //            return new JsonResult(res);
        //        }
        //        if (string.IsNullOrEmpty(password))
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.treadPasswordNull;
        //            return new JsonResult(res);
        //        }
        //        //确认交易账户是否存在
        //        Mt5User mt5User = mt5Service.GetMt5UserById(mt5Id, crmUserModel.Id, (int)Mt5UserType.Real);
        //        if (mt5User == null)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.mt5UserNull;
        //            return new JsonResult(res);
        //        }
        //        if ((obj.Minimum != null && fromamount < obj.Minimum) || (obj.Maximum != null && fromamount > obj.Maximum))
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.depositAmountError;
        //            return new JsonResult(res);
        //        }
        //        if (depositamount <= 0 || fromamount <= 0)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.depositCanNotLessThan0;
        //            return new JsonResult(res);
        //        }
        //        //核对实际支付金额和手续费
        //        RealAmount realAmount = assetsServer.Commission(fromcurrency, depositcurrency, depositamount);
        //        if (realAmount == null)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.exchangeRateNull;
        //            return new JsonResult(res);
        //        }
        //        if (fromamount != realAmount.PayAmount)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.amountInconsistent;
        //            return new JsonResult(res);
        //        }
        //        //确认密码是否正确
        //        string encryptPwd = SecurityUtil.EncryptPwd(password);
        //        if (crmUser.TradePwd != encryptPwd)
        //        {
        //            res.Code = 1;
        //            res.Data = LanguageConfig.tradePwdError;
        //            return new JsonResult(res);
        //        }

        //        CrmDeposit crmDeposit = new CrmDeposit()
        //        {
        //            Id = CommonHelper.GetOrderNo(),
        //            CrmId = crmUserModel.Id,
        //            Login = mt5User.Login,
        //            Mothod = mothod,
        //            FromAmount = depositamount,
        //            FromAmountCurrency = depositcurrency,
        //            ToAmount = fromamount,
        //            ToAmountCurrency = fromcurrency,
        //            ExchangeRate = realAmount.Rate,
        //            DepositAmount = realAmount.PayAmount,
        //            DepositAmountCurrency = fromcurrency,
        //            State = 0,
        //            Fees = realAmount.Commission,
        //            Certificate = certificate,
        //            CreateDate = DateTime.Now,
        //            Info = obj.ToJson()
        //        };
        //        int count = assetsServer.Deposit(ref crmDeposit);
        //        CrmAuditLog crmAuditLog = new CrmAuditLog()
        //        {
        //            OrderId = crmDeposit.Id,
        //            CrmId = crmUserModel.Id,
        //            Type = 4,
        //            Cont = "提交审核",
        //            CreateDate = DateTime.Now
        //        };
        //        crmService.AddAuditLog(crmAuditLog);
        //        res.Code = 0;
        //        res.Data = count > 0 ? LanguageConfig.submitSuccess : LanguageConfig.submitFail;
        //    }
        //    catch (Exception ex)
        //    {
        //        res.Code = 1;
        //        res.Data = ex.Message;
        //    }
        //    return new JsonResult(res);
        //}

        /// <summary>
        /// 提交入金申请
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Deposit(int channelId, int mothod,decimal depositamount, string certificate, string remarks)
        {
            Result res = new Result();
            try
            {
                CrmUserModel crmUserModel = GetSession<CrmUserModel>("CrmUser");
                DepositCollection obj = assetsServer.GetCollection(channelId, mothod);
                if (obj == null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.collectionAccontNull;
                    return new JsonResult(res);
                }
                CrmUser crmUser = crmService.GetCrmUser(crmUserModel.Id);
                //确认用户是否存在
                if (crmUser == null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.crmUserNull;
                    return new JsonResult(res);
                }
                if (string.IsNullOrEmpty(certificate))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.certificateNull;
                    return new JsonResult(res);
                }
                //确认交易账户是否存在
                Mt5User mt5User = mt5Service.GetMt5UserById(crmUserModel.Id);
                if (mt5User == null)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.mt5UserNull;
                    return new JsonResult(res);
                }
                if ((obj.Minimum != null && depositamount < obj.Minimum) || (obj.Maximum != null && depositamount > obj.Maximum))
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.depositAmountError;
                    return new JsonResult(res);
                }
                if (depositamount <= 0 || depositamount <= 0)
                {
                    res.Code = 1;
                    res.Msg = LanguageConfig.depositCanNotLessThan0;
                    return new JsonResult(res);
                }
                CrmDeposit crmDeposit = new CrmDeposit()
                {
                    Id = CommonHelper.GetOrderNo(),
                    CrmId = crmUserModel.Id,
                    Login = mt5User.Login,
                    Mothod = mothod,
                    FromAmount = depositamount,
                    FromAmountCurrency = "USD",
                    ToAmountCurrency = "USD",
                    ExchangeRate = 0,
                    DepositAmount = depositamount,
                    DepositAmountCurrency = "USD",
                    State = 0,
                    Fees = 0,
                    Certificate = certificate,
                    CreateDate = DateTime.Now,
                    Info = obj.ToJson(),
                    Remark= remarks
                };
                int count = assetsServer.Deposit(ref crmDeposit);
                CrmAuditLog crmAuditLog = new CrmAuditLog()
                {
                    OrderId = crmDeposit.Id,
                    CrmId = crmUserModel.Id,
                    Type = 4,
                    Cont = "提交审核",
                    CreateDate = DateTime.Now
                };
                crmService.AddAuditLog(crmAuditLog);
                res.Code = 0;
                res.Msg = count > 0 ? LanguageConfig.submitSuccess : LanguageConfig.submitFail;
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
            }
            return new JsonResult(res);
        }

        #endregion
    }
}
